package com.example.qinghubeta;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Looper;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.example.qinghubeta.Sql.Mysqlist;
import com.example.qinghubeta.Sql.St;
import com.example.qinghubeta.Sql.UserDao;
import com.yatoooon.screenadaptation.ScreenAdapterTools;
import com.zhy.autolayout.AutoLayoutActivity;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends qwe implements View.OnClickListener {

    Button loginbtn,reg;
    TextView user,pwd;
    ImageView auth;
    private SQLiteDatabase w;
    private SQLiteDatabase r;
    private Mysqlist mys;
    private List<St> mdata;
    private String name;
    private String pass;
    SharedPreferences sharedPreferences;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        SharedPreferences sharedPreferences = getSharedPreferences("auth",MODE_PRIVATE);
        initView();
        setclick();
        sqlite();



    }
    private void sqlite() {
        mys = new Mysqlist(this, "zhu_c", null, 1);//使用halper创建数据库
        r=mys.getReadableDatabase();
        w=mys.getWritableDatabase();
        mdata=new ArrayList<St>();
        Cursor query = r.rawQuery("select * from user_mo", null);
        while(query.moveToNext()){
            int index1 = query.getColumnIndex("name");
            int index2 = query.getColumnIndex("pass");
            name = query.getString(index1);
            pass = query.getString(index2);
            mdata.add(new St(0, name, pass));
        }
    }


    private void setclick() {
        auth.setOnClickListener(this);
        loginbtn.setOnClickListener(this);
        reg.setOnClickListener(this);
    }

    private void initView() {
        loginbtn = findViewById(R.id.loginbtn);
        user = findViewById(R.id.loginuser);
        pwd = findViewById(R.id.loginpwd);
        auth = findViewById(R.id.auth);
        reg = findViewById(R.id.reg);
    }

    @Override
    public void onClick(View view) {
        final Intent[] intent = new Intent[1];
        sharedPreferences = getSharedPreferences("auth",MODE_PRIVATE);

        switch (view.getId()){
            case R.id.auth:
                intent[0] = new Intent(getApplicationContext(),Setting.class);
                startActivity(intent[0]);
                break;
            case R.id.loginbtn:
                if (sharedPreferences.getInt("auth",1) == 1){
                    if (user.getText().toString().trim().equals("test") && pwd.getText().toString().trim().equals("test")){
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putString("name","test");
                        editor.apply();
                    intent[0] = new Intent(getApplicationContext(),MainMenu.class);
                    startActivity(intent[0]);
                    }
                }else if (sharedPreferences.getInt("auth",1) == 2) {
                    String name1 = user.getText().toString().trim();
                    String pass1 = pwd.getText().toString().trim();
                    if (name1.equals(name) && pass1.equals(pass)) {

                        Toast.makeText(this, "登录成功", Toast.LENGTH_SHORT).show();
                        SharedPreferences.Editor editor = sharedPreferences.edit();
                        editor.putString("name",name1);
                        editor.apply();
                        intent[0] = new Intent(this, MainMenu.class);
                        startActivity(intent[0]);
                    } else {
                        Toast.makeText(this, "账号或密码不正确", Toast.LENGTH_SHORT).show();
                    }
                }else if (sharedPreferences.getInt("auth",1) == 4) {

                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            String name1 = user.getText().toString().trim();
                            String pass1 = pwd.getText().toString().trim();
                            if (name1.equals("")||pass1.equals("")){
                                Looper.prepare();
                                Toast toast = Toast.makeText(MainActivity.this,"输入不能为空！",Toast.LENGTH_SHORT);
                                toast.show();
                                Looper.loop();
                            }
                            UserDao ud = new UserDao();
                            Boolean result = ud.login(name1,pass1);
                            if (!result){
                                Looper.prepare();
                                Toast toast=Toast.makeText(MainActivity.this,"用户名不存在或密码错误！",Toast.LENGTH_SHORT);
                                toast.show();
                                Looper.loop();
                            }else{
                                Looper.prepare();
                                Toast toast=Toast.makeText(MainActivity.this,"登录成功",Toast.LENGTH_SHORT);
                                toast.show();
                                SharedPreferences.Editor editor = sharedPreferences.edit();
                                editor.putString("name",name1);
                                editor.apply();
                                intent[0] = new Intent(getApplicationContext(), MainMenu.class);
                                startActivity(intent[0]);
                                Looper.loop();

                            }

                            //以上为jdbc登录
                        }
                    }).start();

                }


//
                break;
            case R.id.reg:
                if (sharedPreferences.getInt("auth",1) == 1) {
                   Toast.makeText(getApplicationContext(),"您正在使用演示模式，请更改认证方式",Toast.LENGTH_SHORT).show();
                }else {
                    Intent intent1 = new Intent(this, Register.class);
                    startActivity(intent1);
                }
                break;



        }
    }
}